#include "VxIrrLib.h"

//============================================================================
//! initialize and load from xml file
RCODE UserConfig::Startup( const char * pConfigXmlFileName )
{
	m_strConfigFile = pConfigXmlFileName;
	Reset(); // initialize all values to default
	// read any values set in the config file
	RCODE rc = ReadConfig();
	if( rc )
	{
		// attempt again but write out defaults then read in
		WriteConfig();
		rc = ReadConfig();
	}
	return rc;
}
//============================================================================
//! shutdown
RCODE UserConfig::Shutdown( void )
{
	return 0;
}

//============================================================================
//! reset settings to default
void UserConfig::Reset( void )
{
	// graphics
	m_iScreenWidth			= 800;
	m_iScreenHeight			= 600;
	m_bFullscreen			= false;
	m_f32MouseScaleX		= 1.0f;		// 
	m_f32MouseScaleY		= 1.0f;		// 

	m_bShadows				= false;
	m_iAntiAliasing			= 0;		
	m_iAnisotropicFiltering = 0;
	m_bTrilinearFiltering	= 0;

	// audio
	m_bAudioEnabled			= true;		// if true audio is enabled
	m_f32SoundVolume		= 1.0f;		// master sound effect volume	
	m_f32MusicVolume		= 1.0f;		// master ambient sound volume

	m_bShowTutorial			= true;

	// input
	m_aeKeys[eInputActionMoveForward]	= irr::KEY_KEY_E;
	m_aeKeys[eInputActionMoveBack]		= irr::KEY_KEY_D;
	m_aeKeys[eInputActionSlideLeft]		= irr::KEY_KEY_S;
	m_aeKeys[eInputActionSlideRight]		= irr::KEY_KEY_F;
	m_aeKeys[eInputActionReset]			= irr::KEY_KEY_X;
	m_aeKeys[eInputActionJump]			= irr::KEY_SPACE;

}
//============================================================================
//! read settings from file
RCODE UserConfig::ReadConfig( void )
{
	// open the config xml file
	TiXmlDocument oDocument( m_strConfigFile.c_str() );
	if( 0 == oDocument.LoadFile()) 
	{
		log_msg( 0, "UserConfig: Failed loading config file %s\n", m_strConfigFile.c_str() );
		return -1;
	}
	TiXmlElement * poConfigElement = oDocument.FirstChildElement("config");
	if( NULL == poConfigElement )
	{
		log_msg( 0, "UserConfig: config branch not found\n" );
		return -1;
	}

	// get graphics config
	TiXmlElement * poVideoElement = poConfigElement->FirstChildElement("video");
	if( NULL == poVideoElement )
	{
		log_msg( 0, "UserConfig: video entry not found\n" );
		return -1;
	}
	// graphics
	if( poVideoElement ) 
	{
		int iTemp;
		TiXmlElement * poCurElement;

		// Get screen attributes
		poCurElement = poVideoElement->FirstChildElement("screen");
		if(poCurElement) 
		{
			poCurElement->QueryIntAttribute("width", &m_iScreenWidth);
			poCurElement->QueryIntAttribute("height", &m_iScreenHeight);
			poCurElement->QueryIntAttribute("fullscreen", &iTemp );
			m_bFullscreen = iTemp;
			poCurElement->QueryIntAttribute("resizeable", &iTemp );
			m_bScreenResizeable = iTemp;
		}

		// get graphics attributes
		poCurElement = poVideoElement->FirstChildElement("graphics");
		if(poCurElement) 
		{
			poCurElement->QueryIntAttribute("shadows", &iTemp);
			m_bShadows = iTemp;
			poCurElement->QueryIntAttribute("trilinear", &iTemp);
			m_bTrilinearFiltering = (bool)iTemp;
			poCurElement->QueryIntAttribute("anisotropic", &m_iAnisotropicFiltering);
			poCurElement->QueryIntAttribute( "antialiasing", &m_iAntiAliasing );
		}
	}

	// get audio attributes
	TiXmlElement * poAudioElement = poConfigElement->FirstChildElement("audio");
	if( poAudioElement ) 
	{
		// is audio enabled ?
		poAudioElement->QueryIntAttribute( "enabled", &m_bAudioEnabled);
			
		// get sound volume
		TiXmlElement * poSoundElement = poAudioElement->FirstChildElement("sound");
		if( poSoundElement ) 
		{
			poSoundElement->QueryFloatAttribute( "volume", &m_f32SoundVolume );
		}

		// get music volume
		TiXmlElement * poMusicElement = poAudioElement->FirstChildElement("music");
		if( poMusicElement ) 
		{
			poMusicElement->QueryFloatAttribute( "volume", &m_f32MusicVolume );
		}
	}

	// get tutorial attributes
	TiXmlElement * poTutorialElement = poConfigElement->FirstChildElement("tutorial");
	if( poTutorialElement ) 
	{
		// show tutorial?
		poTutorialElement->QueryIntAttribute( "show_tutorial", &m_bShowTutorial);
		
	}
	return 0;
}
//============================================================================
//! write settings from file
RCODE UserConfig::WriteConfig( void )
{
	TiXmlDocument oDocument;

	// create header
	oDocument.LinkEndChild(new TiXmlDeclaration("1.0", "", ""));
	
	TiXmlElement * poConfigElement = new TiXmlElement("config");
	poConfigElement->SetAttribute("version", "1.0");
	oDocument.LinkEndChild( poConfigElement );

	// video
	TiXmlElement * poVideoElement = new TiXmlElement("video");
	poConfigElement->LinkEndChild( poVideoElement );

	// screen settings
	TiXmlElement * poScreenElement = new TiXmlElement("screen");
	poScreenElement->SetAttribute("width", m_iScreenWidth );
	poScreenElement->SetAttribute("height", m_iScreenHeight );
	poScreenElement->SetAttribute("fullscreen", m_bFullscreen );
	poScreenElement->SetAttribute("resizeable", m_bScreenResizeable );
	poConfigElement->LinkEndChild( poScreenElement );

	// graphics settings
	TiXmlElement * poGraphicsElement = new TiXmlElement("graphics");
	poGraphicsElement->SetAttribute("shadows", m_bShadows );
	poGraphicsElement->SetAttribute("trilinear", m_bTrilinearFiltering );
	poGraphicsElement->SetAttribute("anisotropic", m_iAnisotropicFiltering );
	poGraphicsElement->SetAttribute("antialiasing", m_iAntiAliasing );
	poConfigElement->LinkEndChild( poGraphicsElement );


	// audio element
	TiXmlElement * poAudioElement = new TiXmlElement("audio");
	poAudioElement->SetAttribute("enabled", m_bAudioEnabled );
	poConfigElement->LinkEndChild( poAudioElement );

	// sound settings
	TiXmlElement * poSoundElement = new TiXmlElement("sound");
	poSoundElement->SetDoubleAttribute("volume", m_f32SoundVolume );
	poConfigElement->LinkEndChild( poSoundElement );

	// Music
	TiXmlElement * poMusicElement = new TiXmlElement("music");
	poMusicElement->SetDoubleAttribute("volume", m_f32MusicVolume );
	poConfigElement->LinkEndChild( poMusicElement );

	// tutorial attributes
	TiXmlElement * poTutorialElement = new TiXmlElement("tutorial");
	poTutorialElement->SetAttribute("show_tutorial", m_bShowTutorial );
	poConfigElement->LinkEndChild( poTutorialElement );

	// Write file
	if( false == oDocument.SaveFile( m_strConfigFile.c_str() ) )
	{
		return -1;
	}
	return 0;
}

